var express = require('express');
var router = express.Router();
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;			//本地验证
var queryString = require("querystring");
var url = require("url");

var User = require('../services/web_login_serve');
var Find = require('../services/web_find_serve');
var Menu = require('../services/web_menu_serve');
var web_login_test = require('./web_login_test');




//跳转搜索页
router.get('/', function (req, res) {
	res.redirect('/find');
});

//获取检索页
router.get('/find', function (req, res) {
	var url = req.url
	console.log('初始检索页里的url', url)
	res.render('web_find', {
		title: '无人机管理系统检索',
		arr: [{ sch: 'active', lib: '', abt: '', log: '' }],
		username:req.url.username
	});
});


//处理检索请求
router.get('/search',web_login_test.ensureAuthenticated, function (req, res, next) {

	//解析静态资源中search.js传过来的url参数。
	var searchType = req.query.sType;//搜索类型
	var content = req.query.content;//搜索内容


	Find.findByContent(res, searchType, content, function (err, row, searchType) {
		if (err) {
			return next(err);//使用return XXX 的写法是为了在发错误时不会出现res重复响应状况 
		}
		flag = {}
		if (searchType == 0) {
			flag.uav = true
			flag.order = false
			flag.emplo = false
		}
		else if (searchType == 1) {
			flag.uav = false
			flag.order = true
			flag.emplo = false
		}
		else if (searchType == 2) {
			flag.uav = false
			flag.order = false
			flag.emplo = true
		}
		else {
			console.log('web_find页面的searchType错误')
		}


		res.render('web_find_result', {
			title: '搜索结果-网点管理系统',
			arr: [{ sch: 'active', lib: '', abt: '', log: '' }],
			result: row,
			searchType: flag,
			username:req.url.username
		});

	});
});


module.exports = router

